<?php
/**
 * 楼盘视频信息上传
 */
namespace app\manage\controller;
use \think\Validate;
class BuildingDesc extends \app\common\controller\Manage
{
    /**
     * @param string $data['role']  上传楼盘信息类型，包含video,text,img,album,article
     * @param string $data['building_id']  上传楼盘信息类型
     * @param string $data['title']  信息标题
     * @param string $data['content']  信息内容
     * @param string $data['img']  图文消息背景图
     * @return array
     */
    //新增方法
    public function inserts(){
        $data = input('param.');
        $rule = [
            'role'  => 'require',
            'building_id'  => 'require',
            'title'   => 'require',
            'content'   => 'require',
            'img'   => 'require',
        ];

        $msg = [
            'role.require' => '请选择上传类型',
            'title.require' => '标题不能为空',
            'content.require' => '内容不能为空',
            'img.require' => '图片不能为空',
        ];
        if(!isset($data['img'])){
            unset($rule['img']);
        }
        if(!isset($data['content'])){
            unset($rule['content']);
        }
        $validate   = Validate::make($rule,$msg);
        $result = $validate->check($data);
        if(!$result)
            return [
                'message' => $validate->getError()
            ];
        if(isset($data['id'])){
            //修改当前一条信息
            $ma[] = array('id','eq',$data['id']);
            $resu =  \app\common\model\BuildingDesc::where($ma) -> find();
            $saveData['role'] = $data['role'];
            $saveData['building_id'] = $data['building_id'];
            $saveData['title'] = $data['title'];
            if(isset($data['img'])){
                $res = '';
                $re = $resu -> toArray();
                if(is_string($data['img'])){
                    $mv = ltrim($data['img']);
                    $saveData['img'] = $mv;
                }else{
                    foreach ($data['img'] as $value){
                        $res = $res.','.$value;
                    }
                    $img1 = $re['img'];
                    $re1 = ltrim($res,',');
                    $im = $img1.','.$re1;
                    $mv = ltrim($im,',');
                    $saveData['img'] = $mv;
                }
            }
            if(isset($data['content'])){
                $saveData['content'] = $data['content'];
            }
            \app\common\model\BuildingDesc::where('id',$data['id']) -> update($saveData);
            return [
                'code' => '1',
                'message' => '更新成功',
                'id' => $data['id'],
            ];

        }else{
            //新增数据
            $saveData['role'] =$data['role'];
            $saveData['building_id'] = $data['building_id'];
            $saveData['title'] = $data['title'];
            if(isset($data['img'])){
                $res = '';
                if(is_string($data['img'])){
                    $saveData['img'] = trim($data['img']);
                }else{
                    foreach ($data['img'] as $value){
                        $res = $res.','.$value;
                    }
                    $resu = ltrim($res,',');
                    $saveData['img'] = $resu;
                }
            }
            if(isset($data['content'])){
                $saveData['content'] = $data['content'];
            }
            $desc = \app\common\model\BuildingDesc::create($saveData);

            //上传排序
            $info = \app\common\model\Building::get($data['building_id']);
            $info -> type = ltrim($info['type'].','.$desc -> id,',');
            $info -> save();
            return [
                'code' => '1',
                'message' => '新增成功',
            ];
        }
    }
    //上移下移
    public function shiftUp(){
        $data = input('param.');
        $num = $data['num'];
        foreach ($num as $value){
            $res = $res.','.$value;
        }
        $resu = ltrim($res,',');
        \app\common\model\Building::where('id',$data['id']) -> setField('type',$resu);
        return [
            'code' => '1',
            'message' => '移动成功',
        ];
    }
    //删除当前信息
    public function dels(){
        $id = input('id');
        $buildingInfo = \app\common\model\BuildingDesc::get($id);
        \app\common\model\BuildingDesc::destroy($id);
        //从排序中删除
        $info = \app\common\model\Building::get($buildingInfo['building_id']);
        $typeArr = explode(',',$info['type']);
        foreach ($typeArr as $value){
            if ($value != $id)
                $res = $res.','.$value;
        }
        $info -> type = ltrim($res,',');
        $info -> save();
        return [
            'code' => '1',
            'message' => '删除成功'
        ];
    }
    //删除当前单挑图片
    public function delImg(){
        $data = input('param.');
        $result =  \app\common\model\BuildingDesc::get($data['id']);
        $arr = explode(',',$result['img']);
        for($i=0;$i<count($arr);$i++){
            $res = $arr[$i];
            if($res !== $data['img']){
                $resu .= ','.$res;
            }
        }
        $resul = ltrim($resu,',');
        \app\common\model\BuildingDesc::Where('id',$data['id']) -> setField('img',$resul);
        return [
            'code' => '1',
            'message' => '删除成功'
        ];
    }
}